﻿using System.Collections.Generic;
using System.Data;
using Smatime.Web.DAO.SmatimeDatasetTableAdapters;
using Smatime.Web.Models;

namespace Smatime.Web.DAO
{
    public class CompanyStoreRepository :
        BaseRepository<CompanyStore, SmatimeDataset.CompanyStoreDataTable, CompanyStoreTableAdapter>
    {
        /// <summary>
        ///     check name exit
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CompanyStore GetByStoreName(long Id, string name)
        {
            using (var adapter = new CompanyStoreTableAdapter())
            {
                var list = new List<CompanyStore>();
                using (var table = adapter.GetDataByStoreName(Id, name))
                {
                    var row = table.Rows[0];
                    return (CompanyStore) (new CompanyStore().FromDataRow(row));
                }
            }
        }

        /// <summary>
        ///     check name exit
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CompanyStore GetByStoreId(long CompanyId, long Id)
        {
            using (var adapter = new CompanyStoreTableAdapter())
            {
                var list = new List<CompanyStore>();
                using (var table = adapter.GetDataByStoreId(CompanyId, Id))
                {
                    var row = table.Rows[0];
                    return (CompanyStore) (new CompanyStore().FromDataRow(row));
                }
            }
        }

        /// <summary>
        ///     get store by companyid
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IEnumerable<CompanyStore> GetStoreByCompanyId(long id)
        {
            using (var adapter = new CompanyStoreTableAdapter())
            {
                using (var table = adapter.GetDataByCompanyId(id))
                {
                    var list = new List<CompanyStore>();
                    foreach (DataRow row in table.Rows)
                    {
                        var storeOfCompany = new CompanyStore();
                        storeOfCompany.FromDataRow(row);
                        list.Add(storeOfCompany);
                    }
                    return list;
                }
            }
        }
    }
}